package com.cn.parks.dao.impl;

import com.cn.parks.dao.FixedParkingDao;
import com.cn.parks.entity.FixedParking;
import com.cn.parks.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 固定车位停车记录数据访问实现类
 * 实现对固定车位停车记录表的增删改查等基本操作
 */
public class FixedParkingDaoImpl implements FixedParkingDao {
    
    /**
     * 添加固定车位停车记录
     * @param fixedParking 固定车位停车记录对象
     * @return 影响的行数，1表示添加成功，0表示添加失败
     * @throws Exception SQL执行异常
     */
    @Override
    public int insert(FixedParking fixedParking) throws Exception {
        String sql = "INSERT INTO fixed_parking (fixed_id, card_id, entry_date, entry_time, out_date, out_time) VALUES (?, ?, ?, ?, ?, ?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, fixedParking.getFixedId());
            pstmt.setString(2, fixedParking.getCardId());
            pstmt.setDate(3, new java.sql.Date(fixedParking.getEntryDate().getTime()));
            pstmt.setTime(4, new java.sql.Time(fixedParking.getEntryTime().getTime()));
            pstmt.setDate(5, fixedParking.getOutDate() != null ? new java.sql.Date(fixedParking.getOutDate().getTime()) : null);
            pstmt.setTime(6, fixedParking.getOutTime() != null ? new java.sql.Time(fixedParking.getOutTime().getTime()) : null);
            return pstmt.executeUpdate();
        } finally {
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 根据ID删除固定车位停车记录
     * @param fixedId 固定车位停车记录ID
     * @return 影响的行数，1表示删除成功，0表示删除失败
     * @throws Exception SQL执行异常
     */
    @Override
    public int deleteById(String fixedId) throws Exception {
        String sql = "DELETE FROM fixed_parking WHERE fixed_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, fixedId);
            return pstmt.executeUpdate();
        } finally {
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 更新固定车位停车记录
     * @param fixedParking 固定车位停车记录对象
     * @return 影响的行数，1表示更新成功，0表示更新失败
     * @throws Exception SQL执行异常
     */
    @Override
    public int update(FixedParking fixedParking) throws Exception {
        String sql = "UPDATE fixed_parking SET card_id=?, entry_date=?, entry_time=?, out_date=?, out_time=? WHERE fixed_id=?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, fixedParking.getCardId());
            pstmt.setDate(2, new java.sql.Date(fixedParking.getEntryDate().getTime()));
            pstmt.setTime(3, new java.sql.Time(fixedParking.getEntryTime().getTime()));
            pstmt.setDate(4, fixedParking.getOutDate() != null ? new java.sql.Date(fixedParking.getOutDate().getTime()) : null);
            pstmt.setTime(5, fixedParking.getOutTime() != null ? new java.sql.Time(fixedParking.getOutTime().getTime()) : null);
            pstmt.setString(6, fixedParking.getFixedId());
            return pstmt.executeUpdate();
        } finally {
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 根据ID查询固定车位停车记录
     * @param fixedId 固定车位停车记录ID
     * @return 固定车位停车记录对象，如果不存在返回null
     * @throws Exception SQL执行异常
     */
    @Override
    public FixedParking getById(String fixedId) throws Exception {
        String sql = "SELECT * FROM fixed_parking WHERE fixed_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, fixedId);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                return extractFixedParkingFromResultSet(rs);
            }
            return null;
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 查询所有固定车位停车记录
     * @return 固定车位停车记录列表
     * @throws Exception SQL执行异常
     */
    @Override
    public List<FixedParking> getAll() throws Exception {
        String sql = "SELECT * FROM fixed_parking";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<FixedParking> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(extractFixedParkingFromResultSet(rs));
            }
            return list;
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 根据卡ID查询固定车位停车记录
     * @param cardId 卡ID
     * @return 固定车位停车记录列表
     * @throws Exception SQL执行异常
     */
    @Override
    public List<FixedParking> getByCardId(String cardId) throws Exception {
        String sql = "SELECT * FROM fixed_parking WHERE card_id = ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<FixedParking> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, cardId);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(extractFixedParkingFromResultSet(rs));
            }
            return list;
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 根据日期范围查询固定车位停车记录
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 固定车位停车记录列表
     * @throws Exception SQL执行异常
     */
    @Override
    public List<FixedParking> getByDateRange(Date startDate, Date endDate) throws Exception {
        String sql = "SELECT * FROM fixed_parking WHERE entry_date BETWEEN ? AND ?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<FixedParking> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setDate(1, new java.sql.Date(startDate.getTime()));
            pstmt.setDate(2, new java.sql.Date(endDate.getTime()));
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(extractFixedParkingFromResultSet(rs));
            }
            return list;
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) DBUtil.closeConnection(conn);
        }
    }

    /**
     * 从ResultSet中提取FixedParking对象的辅助方法
     * @param rs ResultSet结果集
     * @return FixedParking对象
     * @throws Exception SQL执行异常
     */
    private FixedParking extractFixedParkingFromResultSet(ResultSet rs) throws Exception {
        FixedParking fixedParking = new FixedParking();
        fixedParking.setFixedId(rs.getString("fixed_id"));
        fixedParking.setCardId(rs.getString("card_id"));
        fixedParking.setEntryDate(rs.getDate("entry_date"));
        fixedParking.setEntryTime(rs.getTime("entry_time"));
        fixedParking.setOutDate(rs.getDate("out_date"));
        fixedParking.setOutTime(rs.getTime("out_time"));
        return fixedParking;
    }
} 